package com.scanner;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.view.SurfaceView;

public class ScannerGraphics extends SurfaceView {

	CenterButton captureButton;	
    ButtonView buttonView;
    ContourView contourView;
    DebugView debugView;
    TargetA4 targetView;
	
    //events handling
    ArrayList<ScannerEventListener> scannerToggleEventObservers = new ArrayList<ScannerEventListener>();
    
    public void SetEventObserver(ScannerEventListener observer){
    	scannerToggleEventObservers.add(observer);
    }
    
    public void RaiseToggleScannerEvent(boolean set, String method)
    {
    	for (ScannerEventListener eventListner: scannerToggleEventObservers)
    	{
    		eventListner.ScannerEventCallback(set,method);
    	}
    }
    ///
    
	public ScannerGraphics(Context context) {
		super(context);

		captureButton = new CenterButton(context);
		buttonView = new ButtonView(context, captureButton);
		contourView = new ContourView(context, getContourFromAlgo());
		debugView = new DebugView(context);
		targetView = new TargetA4(context);
		
		setWillNotDraw(false);
		
		contourView.SetEventObserver(new ScannerEventListener(){
        	@Override
			public void ScannerEventCallback(boolean flag, String method)
        	{
        		RaiseToggleScannerEvent(flag, method);
        	}
        });
	}


    private FrameContour getContourFromAlgo()
    {    	
    	FrameContour contour = new FrameContour();
    	
    	return contour;
    }
    
    public void UpdateContour(int[] points)
    {
    	if (points == null)
    	{
    		return;
    	}    	
    	contourView.UpdateContour(new Point(points[0],points[1]),new Point(points[2],points[3]),
    								new Point(points[4],points[5]),new Point(points[6],points[7]));
    }
     public void UpdateText(String method)
    {
    	debugView.UpdateText(method);
    }
   
        
    @Override
    public void onDraw(Canvas canvas)
    {
    	buttonView.draw(canvas);
    	contourView.draw(canvas);
    	debugView.draw(canvas);
    	targetView.draw(canvas);
    }
    
    public void ClearView()
    {
    	contourView.ClearView();
    }
}
